#pragma once
#include "../stdafx.h"
#include  "../modelheaders.h"
#include "../math/math.h"
#include <vector>
using namespace std;
namespace SLG
{
	template<typename T>
	class Manager
	{
	public:
		Manager(char* fname)
		{
			char logname[256];
			strcpy_s(logname,256,fname);
			strcat_s(logname,256,".log");
			m_fp=fopen(logname,"w+");
			m_data.clear();
			m_cur=0;
		}
		~Manager(void)
		{
			fclose(m_fp);
		}

		//virtual bool AddObj(int32,char*,void*)=0;
		virtual void Log()=0;
		//virtual bool DelObj(uint32);
		
		virtual T* GetObj(int32 id)
		{
			for(uint32 i=0;i<m_data.size();i++)
			{
				if(m_data[i].id==id)
					return &m_data[i];
			}
			return NULL;
		}

		inline virtual void _log(char* msg)
		{
			fwrite(msg,strlen(msg),1,m_fp);
		}

		inline virtual void Clear()
		{
			m_data.clear();
		}

		vector<T> m_data;
		int32 m_cur;
		FILE* m_fp;
	};

	
	typedef struct _OBJ
	{
		int32 id;
		int32 mid;
		int32 tid;
		char* name;
		Model* model;
	}OBJ;

	typedef struct _VERTEXBUFFER
	{
		LPDIRECT3DVERTEXBUFFER9 pVB;
		int32 count;
	}VERTEXBUFFER;

	typedef struct _FIGURE
	{
		int32 id;
		int32 fid;
		int32 mid;
		char* name;
		int32 cell_x;
		int32 cell_y;
		FACE face;
		VERTEXBUFFER VB[16];
		AABB* aabb;
		int32 nVB;
	}FIGURE;

	typedef struct _TEXTURE
	{
		int32 id;
		char* name;
		LPDIRECT3DTEXTURE9 tex;
	}TEXTURE;

	typedef struct _ANIMATION
	{
		int32 id;
		int32 fid;
		char* name;
		int32 anim;
		D3DXVECTOR3* vertices;
	}ANIMATION;

	class ModelManager :
		public Manager<OBJ>
	{
	public:
		ModelManager(void);
		~ModelManager(void);
		bool AddObj(int32,int32,char*,void*);
		Model* GetModel(int32);
		int32 HasObj(int32);
		void Log();

	};

	class FigureManager:
		public Manager<FIGURE>
	{
	public:
		FigureManager(void);
		~FigureManager(void);
		bool AddObj(int32,int32,const char*,int32,int32,FACE);
		bool AddVB(int32,LPDIRECT3DVERTEXBUFFER9,int32);
		D3DXVECTOR3 GetMidPos(int32 id);
		int32 GetFidFromCell(int32,int32);
		bool ClearVB();
		int32 HasObj(int32);
		void Log();
		int32 GetMid(int32);
	};

	class TextureManager:
		public Manager<TEXTURE>
	{
	public:
		TextureManager(void);
		~TextureManager(void);
		int32 AddObj(char*,LPDIRECT3DTEXTURE9);
		int32 HasObj(char*);
		void Log();
	};

	class AnimManager:
		public Manager<ANIMATION>
	{
	public:
		AnimManager(ModelManager*,FigureManager*);
		~AnimManager(void);
		bool AddObj(int32,char *,int32);
		void CalcAnim(DWORD time);
		void CalcBones(Model* m,size_t anim, size_t time);
		bool ChangeAnim(int32,int32);
		void AnimatePreModel(Model* m,size_t anim,D3DXVECTOR3**,DWORD time);
		void Log();

		FigureManager* m_fManager;
		ModelManager* m_mManager;
	};
}